<template>
  <el-form ref="ruleFormRef" :rules="rules" :model="form" label-width="auto"
    style="max-width: 600px;padding: 20px 0 0 20px;">
    <el-form-item label="入库编号">
      <el-input v-model="form.warehouseInNo" disabled />
    </el-form-item>
    <el-form-item label="来源编号">
      <el-input v-model="form.sourceNo" disabled />
    </el-form-item>
    <el-form-item label="入库类型">
      <el-select v-model="form.goodsType" disabled>
        <el-option v-for="dict in warehouse_in_store_type" :key="dict.value" :label="dict.label"
          :value="parseInt(dict.value)" />
      </el-select>
    </el-form-item>
    <el-form-item label="物品编号">
      <el-input v-model="form.goodsNo" disabled />
    </el-form-item>
    <el-form-item label="物品名称">
      <el-input v-model="form.goodsName" disabled />
    </el-form-item>
    <el-form-item label="规格型号">
      <el-select v-model="form.specification" placeholder="请选择规格型号" disabled>
        <el-option v-for="dict in model_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="仓库名称" prop="warehouseId">
      <el-select v-model="form.warehouseId" placeholder="请选择仓库" clearable>
        <el-option v-for="item in warehouseList" :key="item.id" :label="item.warehouseName" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="入库数量">
      <el-input v-model="form.warehouseInNumber" disabled />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
        :disabled="inStockStore.warehouseInInfo[inStockStore.index].warehouseId != null">确认</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { listStore, warehouseInStockInfo, updateWarehouseId, updateWarehouseStatus } from '@/api/stock/inStock'
import useInStockStore from '@/store/modules/inStock'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const { proxy } = getCurrentInstance()
const { warehouse_in_store_type, model_type } = proxy.useDict('warehouse_in_store_type', 'model_type')


const route = useRoute()
const router = useRouter()

const inStockStore = useInStockStore()
const ruleFormRef = ref()

let warehouseList = ref()

const form = ref({
  warehouseInNo: '',
  sourceNo: '',
  goodsType: '',
  goodsNo: '',
  goodsName: '',
  specification: '',
  warehouseId: '',
  warehouseInNumber: ''
})

const rules = ref({
  warehouseId: [
    { required: true, message: '请选择仓库', trigger: 'blur' }
  ]
})

const onSubmit = () => {
  inStockStore.warehouseInInfo[inStockStore.index].warehouseId = form.value.warehouseId
  updateWarehouseId(route.query.warehouseInInfoId, form.value.warehouseId, form.value.goodsNo, form.value.warehouseInNumber).then(response => {
    ElMessage({
      type: 'success',
      message: '入库成功'
    })
    let count = 0
    for(let i = 0;i < inStockStore.warehouseInInfo.length;i++) {
      if(inStockStore.warehouseInInfo[i].warehouseId != null) {
        count++
      }
    }
    if(count == inStockStore.warehouseInInfo.length) {
      inStockStore.warehouseInStatus = 2
      updateWarehouseStatus(inStockStore.warehouseInId)
    }
    router.push('/stock/inInfo')
  })
}

const cancel = () => {
  router.push('/stock/inInfo')
}

const submitForm = async (formEl) => {
  if(!formEl) return
  await formEl.validate((valid, fields) => {
    if(valid) {
      onSubmit()
    }
  })
}



onMounted(() => {
  listStore().then(response => {
    warehouseList.value = response.rows
  })

  warehouseInStockInfo(route.query.warehouseInInfoId, inStockStore.goodsType).then(response => {
    form.value.warehouseInNo = response.data.warehouseInNo
    form.value.sourceNo = response.data.sourceNo
    form.value.goodsType = response.data.goodsType
    form.value.goodsNo = response.data.goodsNo
    form.value.goodsName = response.data.goodsName
    form.value.specification = response.data.specification
    form.value.warehouseId = response.data.warehouseId
    form.value.warehouseInNumber = response.data.warehouseInNumber
  })
})
</script>

<style scoped></style>